package com.drpclub.gamebackend.controller;

import com.drpclub.gamebackend.bean.RepeatImg;
import com.drpclub.gamebackend.common.constant.ActionUri;
import com.drpclub.gamebackend.common.constant.Const;
import com.drpclub.gamebackend.common.constant.RootUri;
import com.drpclub.gamebackend.common.utils.DrpUtils;
import com.drpclub.gamebackend.common.utils.FileUtils;
import com.drpclub.gamebackend.common.utils.JwtUtils;
import com.drpclub.gamebackend.common.utils.NetworkResult;
import com.drpclub.gamebackend.common.utils.log.RsHelper;
import com.drpclub.gamebackend.repository.ResourceRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;

import static com.drpclub.gamebackend.common.constant.Const.*;
import static com.drpclub.gamebackend.common.utils.DrpUtils.getUsernameFromCookie;

/**
 * @author ：drp
 * @date ：Created in 2020/5/10 22:50
 */
@Slf4j
@Controller
@RequestMapping(RootUri.FILE)
public class FileController {
    @Resource
    private FileUtils fileUtils;

    @Resource
    private ResourceRepository resourceRepository;

    @GetMapping(ActionUri.GET_HEADER)
    public String getHeaderFile(String filename, String username) {
        if (filename.equals (DEFAULT_IMAGE)) {
            return String.format ("%s%s/%s", ACCESSPATH, Const.HEAD_IMAGE_PATH, filename);
        }
        return String.format ("%s%s/%s/%s", ACCESSPATH, Const.HEAD_IMAGE_PATH, username, filename);
    }

    @GetMapping(ActionUri.GET_COVER)
    public String getCoverFile(String filename, String username) {
        if (filename.equals (DEFAULT_IMAGE)) {
            return String.format ("%s%s/%s", ACCESSPATH, Const.COVER_IMAGE_PATH, filename);
        }
        return String.format ("%s%s/%s/%s", ACCESSPATH, Const.COVER_IMAGE_PATH, username, filename);
    }

    /**
     * 未使用
     */
    @GetMapping(ActionUri.GET_CONTENT)
    public String getContentFile(String filename, String contentId, String username) {
        if (filename.equals (DEFAULT_IMAGE)) {
            return String.format ("%s%s/%s", ACCESSPATH, Const.CONTENT_IMAGE_PATH, filename);
        }
        return String.format ("%s%s/%s/%s/%s", ACCESSPATH, Const.CONTENT_IMAGE_PATH, username, contentId, filename);
    }


    @GetMapping(ActionUri.GET_CONTENT_HTML)
    public String getContentHtml(String contentId) {
        String username = resourceRepository.findUsernameById (contentId);
        return String.format ("%s%s/%s/%s.html", ACCESSPATH, Const.CONTENT_ESSAY_PATH, username, contentId);
    }

    @ResponseBody
    @PostMapping(ActionUri.REPEAT_IMAGE)
    public Callable<NetworkResult<Object>> deleteRepeatImage(@RequestBody RepeatImg repeatImg,
                                                             HttpServletRequest request) {
        return () -> {
            String token = DrpUtils.getToken (request);
            String username = JwtUtils.getUsernameFromToken (token);
            List<String> imgUrIs = repeatImg.getImgUrIs ();
            imgUrIs.forEach (url -> {
                String params = url.split ("\\?")[1];
                String[] param = params.split ("&");
                final String path =
                        fileUtils.getFileSaveRealPath (CONTENT_IMAGE_PATH, true) + "/" +
                                username + "/" +
                                repeatImg.getContentId () + "/" +
                                //filename
                                param[0].split ("=")[1];
                File file = new File (path);
                if (file.exists ()) {
                    file.delete ();
                }
            });
            return RsHelper.success (true);
        };
    }
}
